package streamTest;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
 * @program: javase
 * @description:
 * @author: lld
 * @create: 2021-03-17 22:15
 **/
public class Demo1 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 3, 4, 5, 6, 7, 8};
        int array = Arrays.stream(arr).filter(item -> item < 5).distinct().sum();
        System.out.println(array);


// 初始化
        List<Student> students = new ArrayList<Student>() {
            {
                add(new Student(20162001, "仲谋", 22, 3, "土木工程", "浙江大学"));
                add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学"));
                add(new Student(20160004, "云长", 21, 2, "信息安全", "武汉大学"));
                add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学"));
                add(new Student(20160002, "伯约", 21, 2, "信息安全", "武汉大学"));
                add(new Student(20161002, "元直", 23, 4, "土木工程", "华中科技大学"));
                add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学"));
                add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学"));
                add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学"));
                add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学"));
                add(new Student(20160001, "孔明", 20, 1, "土木工程", "武汉大学"));
            }
        };
//        System.out.println("students.stream().filter((item)-> item.getId()%2==0).collect(Collectors.toList()) = "
//                + students.stream().filter((item) -> item.getId() % 2 == 0).collect(Collectors.toList()));
//        System.out.println("students.stream().filter(item-> item.getAge()>21).collect(Collectors.toList()) = "
//                + students.stream().filter(item -> item.getAge() > 21).collect(Collectors.toList()));
        students.stream().sorted((x, y) -> (int) (x.getId() - y.getId()))
                .forEach(System.out::println);

        Student student = students.stream().max((x, y) -> x.getGrade() - y.getGrade()).get();
        System.out.println("student = " + student);

        List<Student> collect = students.stream()
                .filter(item -> item.getAge() > 22)//过滤
                .sorted(((o1, o2) -> o2.getGrade() - o1.getGrade()))//排序
                .collect(Collectors.toList());

        List<String> majorList = students.stream()
                .map(Student::getMajor)//提取major
                .collect(Collectors.toList());

//        提取对象的id作为集合
        List<Long> idList = students.stream()
                .map(Student::getId)
                .collect(Collectors.toList());

        System.out.println("idList" + idList);

//        集合转数组
        long[] longs = idList.stream()
                .mapToLong(Long::longValue)
                .sorted()
                .toArray();

        System.out.println("longs" + Arrays.toString(longs));

        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        int[] array1 = list.stream().mapToInt(item -> item.intValue()).toArray();

//        int[] 不能直接转String[]
//        Integer array2={1,2,3};
//        Arrays.sort() 倒序的话 用Comparator 必须为Integer
        Collections.sort(list, (x, y) -> y - x);
        String[] s = {"aaa"};
        Arrays.sort(s, (a, b) -> (a + b).compareTo(b + a));

        System.out.println(collect);
        System.out.println(majorList);
        System.out.println("array1" + Arrays.toString(array1));

//        stream流并行计算，效率高
//        计算1+..+10_0000_0000
        long sum = LongStream.range(0L, 10_0000_0000L).parallel().reduce(0, Long::sum);
        System.out.println("sum = " + sum);
        System.out.println("Long.MAX_VALUE = " + Long.MAX_VALUE);
    }
}

class Student {

    /**
     * 学号
     */
    private long id;

    private String name;

    private int age;

    /**
     * 年级
     */
    private int grade;

    /**
     * 专业
     */
    private String major;

    /**
     * 学校
     */
    private String school;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public Student(long id, String name, int age, int grade, String major, String school) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.grade = grade;
        this.major = major;
        this.school = school;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", grade=" + grade +
                ", major='" + major + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
}


